package org.nuist.controller;

import org.nuist.pojo.User;
import org.nuist.service.user.UserServiceImpl;
import org.nuist.utils.JsonResult;
import org.nuist.utils.PageJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    UserServiceImpl userService;

    /**
     * 登录操作
     * @author DYH
     * @param user
     * @return JsonResult
     */
    @RequestMapping(value = "/userLogin",method = RequestMethod.POST)
    public JsonResult login(@RequestBody User user){
        User getUser = userService.findUserByName(user.getUserName());
        if (getUser != null) {
            if (user.getUserName().equals(getUser.getUserName()) && user.getPassword().equals(getUser.getPassword())) {
                try {
                    if (!getUser.getPerms().equals("available")) {
                        return JsonResult.fail().set("msg", "当前账户不可用!");
                    }
                }catch (Exception e){
                    //pass
                }
                int id = getUser.getUserId();
                return JsonResult.ok().set("userid", id);
            }
            return JsonResult.fail().set("msg","登录失败,用户名与密码不匹配!");
        }
        return JsonResult.fail().set("msg","登录失败,不存在该账号！");
    }




}
